
public class MultistatePanel extends Component{
	
	stateButton map, graph, chart, song;
	int lastUpdateFilterResultsCalled=-1;

	public MultistatePanel(int x, int y, int w, int h) {
		this.x = x;
		this.y = y;
		this.width = w;
		this.height = h;
		chart = new stateButton("Charts", true,x + 50, y+10, 70, 25);
		map = new stateButton("Map", false,x + 120, y+10, 70, 25);
		graph = new stateButton("Graph", false,x + 190, y+10, 70, 25);
		song = new stateButton("Songs", false, x+260, y+10, 70, 25);
	}
	
	public void draw() {
		
		GLOBAL.processing.rectMode(GLOBAL.processing.CORNER);
		GLOBAL.processing.stroke(80);
		GLOBAL.processing.fill(GLOBAL.processing.color(50,50,50,255));
		GLOBAL.processing.ellipse(chart.x, chart.y + (float)chart.height/2, chart.height, chart.height);
		GLOBAL.processing.ellipse(song.x+song.width, song.y + (float)song.height/2, song.height, song.height);
		GLOBAL.processing.noStroke();
		GLOBAL.processing.rect(chart.x, chart.y, chart.width + graph.width + graph.width + song.width, chart.height);
		GLOBAL.processing.stroke(80);
		GLOBAL.processing.line(chart.x, chart.y, chart.x + chart.width + graph.width + graph.width + song.width, chart.y);
		GLOBAL.processing.line(chart.x, chart.y + chart.height, chart.x + chart.width + graph.width + graph.width + song.width, chart.y + chart.height);
		GLOBAL.processing.line(map.x, map.y, map.x, map.y + map.height);
		GLOBAL.processing.line(graph.x, graph.y, graph.x, graph.y + graph.height);
		GLOBAL.processing.line(song.x,song.y,song.x,song.y + song.height);
		
		chart.draw();
		map.draw();
		graph.draw();
		song.draw();
	}
	
	public void mousePressed() {

		if (chart.mouseOver()) {
			if (GLOBAL.analysisType == 0) {
				return;
			}
			else if (!chart.state) {
				reset();
				GLOBAL.lastfm_index_start = 0;
				GLOBAL.analysisType = 0;
				chart.state = true;
				if(lastUpdateFilterResultsCalled != GLOBAL.updateFilterResultsCalled)
					GLOBAL.updateChartsFilterResults();
				lastUpdateFilterResultsCalled = GLOBAL.updateFilterResultsCalled;

			}
		}
		else if(map.mouseOver()) {
			if (GLOBAL.analysisType == 1) {
				return;
			}
			else if (!map.state) {
				reset();
				GLOBAL.analysisType = 1;
				map.state = true;
				if(lastUpdateFilterResultsCalled != GLOBAL.updateFilterResultsCalled)
					GLOBAL.updateMapFilterResults();
				lastUpdateFilterResultsCalled = GLOBAL.updateFilterResultsCalled;
			}
		}
		else if (graph.mouseOver()) {
			if (GLOBAL.analysisType == 2) {
				return;
			}
			else if (!graph.state) {
				reset();
				GLOBAL.graphsArea.networkGraph.loadData();
				GLOBAL.analysisType = 2;
				graph.state = true;
			}
		}
		else if(song.mouseOver()) {
			if (GLOBAL.analysisType == 3) {
				return;
			}
			else if (!song.state) {
				reset();
				GLOBAL.analysisType = 3;
				song.state = true;

				// TODO update songs
			}
		}
	}
	
	public void reset() {
		map.state = false;
		graph.state = false;
		chart.state = false;
		song.state = false;
	}
	
	public class stateButton extends Component{
		
		String label;
		boolean state;
		
		public stateButton(String label, boolean state, int x, int y, int w, int h) {
			this.x = x;
			this.y = y;
			this.width = w;
			this.height = h;
			this.label = label;
			this.state = state;
		}
		
		public void draw() {
			GLOBAL.processing.textFont(GLOBAL.plotFont, 14);
			int cx = x;
			int cy = y;
			if(label != null)
			{
				GLOBAL.processing.textAlign(GLOBAL.processing.CENTER);
				GLOBAL.processing.strokeWeight(1);

				if(state){
					if (GLOBAL.analysisType == 0) {
						GLOBAL.processing.rectMode(GLOBAL.processing.CORNER);
						GLOBAL.processing.noStroke();
						GLOBAL.processing.fill(GLOBAL.processing.color(80,80,80,255));
						GLOBAL.processing.ellipse(x, y + (float)graph.height/2, height, height);
						GLOBAL.processing.rect(x, y, width, height);
					}
					else if (GLOBAL.analysisType == 1) {
						GLOBAL.processing.rectMode(GLOBAL.processing.CORNER);
						GLOBAL.processing.noStroke();
						GLOBAL.processing.fill(GLOBAL.processing.color(80,80,80,255));
						GLOBAL.processing.rect(x, y, width, height);
					}
					else if (GLOBAL.analysisType == 2) {
						GLOBAL.processing.rectMode(GLOBAL.processing.CORNER);
						GLOBAL.processing.noStroke();
						GLOBAL.processing.fill(GLOBAL.processing.color(80,80,80,255));
						GLOBAL.processing.rect(x, y, width, height);
					}
					else if (GLOBAL.analysisType == 3) {
						GLOBAL.processing.rectMode(GLOBAL.processing.CORNER);
						GLOBAL.processing.noStroke();
						GLOBAL.processing.fill(GLOBAL.processing.color(80,80,80,255));
						GLOBAL.processing.ellipse(x+width, y + (float)graph.height/2, height, height);
						GLOBAL.processing.rect(x, y, width, height);
					}
				}
				
				if (label.equalsIgnoreCase("chart")) cx -= 5;
				else if (label.equalsIgnoreCase("songs")) cx += 5;
				
				GLOBAL.processing.strokeWeight(1);
				GLOBAL.processing.fill(GLOBAL.textColor);
				GLOBAL.processing.text(label, cx + width/2, cy + 18);
			}
		}
		
	}
	
}
